Docker 的安装和常用命令

安装 Docker

安装依赖

1sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common

添加 docker 的 GPG key

1curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/docker.gpg

添加 docker 的 APT 源并更新

1sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
2sudo apt update

安装 docker

1sudo apt install docker-ce docker-ce-cli containerd.io
  • ce 表示社区版(Community Edition)
  • ee 表示企业版(Enterprise Edition)

查看是否安装成功

1$ sudo docker version
2Client: Docker Engine - Community
3 Version:           20.10.23
4 API version:       1.41
5 Go version:        go1.18.10
6 Git commit:        7155243
7 Built:             Thu Jan 19 17:45:08 2023
8 OS/Arch:           linux/amd64
9 Context:           default
10 Experimental:      true
11
12Server: Docker Engine - Community
13 Engine:
14  Version:          20.10.23
15  API version:      1.41 (minimum version 1.12)
16  Go version:       go1.18.10
17  Git commit:       6051f14
18  Built:            Thu Jan 19 17:42:57 2023
19  OS/Arch:          linux/amd64
20  Experimental:     false
21 containerd:
22  Version:          1.6.16
23  GitCommit:        31aa4358a36870b21a992d3ad2bef29e1d693bec
24 runc:
25  Version:          1.1.4
26  GitCommit:        v1.1.4-0-g5fd4c4d
27 docker-init:
28  Version:          0.19.0
29  GitCommit:        de40ad0

代理配置

创建或编辑 /etc/systemd/system/docker.service.d/http-proxy.conf 文件,写入以下内容:

1[Service]
2Environment="ALL_PROXY=http://192.168.1.100:7890"
3Environment="HTTP_PROXY=http://192.168.1.100:7890"
4Environment="HTTPS_PROXY=http://192.168.1.100:7890"

然后重启:

1sudo systemctl daemon-reload
2sudo systemctl restart docker

常用命令

  • 安装镜像: image pull [OPTIONS] NAME[:TAG|@DIGEST]
  • 删除镜像: image rm [OPTIONS] IMAGE [IMAGE...]
  • 运行容器: container run [OPTIONS] IMAGE [COMMAND] [ARG...]
  • 终止容器: container stop [OPTIONS] CONTAINER [CONTAINER...]

运行容器时通常使用前台和后台两种模式:

  • 前台模式通常使用 -it 选项,其中:
    • -i, --interactive 表示保持 STDIN 打开,
    • -t, --tty 表示分配一个伪终端
  • 后台模式通常使用 -d -p <host-port>:<container-port> 选项,其中:
    • -d, --detach 表示后台运行
    • -p, --publish 表示将容器端口公开到宿主机上

创建镜像

使用 commit 命令可以创建镜像,然后通过 save 命令可以将镜像保存到指定位置,通过 load 命令可以加载镜像文件。

  • 创建镜像: container commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
  • 保存镜像: image save,默认写到 stdout,可以通过 -o 指定文件
  • 加载镜像: image load,默认读取 stdint,可以通过 -i 指定文件

自动补全的问题

使用 ubuntu 镜像时,会发现很多命令无法自动补全,需要安装 bash-completion

1apt install bash-completion
2source /etc/bash_completion

然后编辑 /etc/bash.bashrc 删除相关行的注释:

1# enable bash completion in interactive shells
2if ! shopt -oq posix; then
3  if [ -f /usr/share/bash-completion/bash_completion ]; then
4    . /usr/share/bash-completion/bash_completion
5  elif [ -f /etc/bash_completion ]; then
6    . /etc/bash_completion
7  fi
8fi